package android.longs.study.user.service;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;


import android.longs.study.base.ResponeModel;
import android.longs.study.user.dao.user.UserDao;
import android.longs.study.user.dao.user.UserDaoImpl;
import android.longs.study.user.model.sub.UserSubjectModel;
import android.longs.study.user.model.user.UserModel;
import android.longs.study.user.service.in.UserService;
import android.longs.study.utils.GsonUtils;

public class UserServiceImple implements UserService {

    private  UserDao userDao = new UserDaoImpl();

    /**
     * 注册
     */
    @Override
    public String registerFunction(HttpServletRequest request) {
	String resultString = "";


	ResponeModel<UserModel> responeModel = new ResponeModel<UserModel>();

	/*
	 *  	keyMap.put("name",emalString);
                keyMap.put("nike",nikString);
                keyMap.put("sex",selectedItem);
                keyMap.put("password",passwordString);
	 */
	String userName = request.getParameter("name");
	String nike = request.getParameter("nike");
	String sex = request.getParameter("sex");
	String password = request.getParameter("password");
	String registerType = request.getParameter("registerType");
	String userTitleImage = request.getParameter("userTitleImage");

	System.out.println("name "+userName+"  nike  "+nike+"  sex "+sex+"  password "+password);

	//查询用户是否存在 
	UserModel queryModel = userDao.queryModelFormName(userName);
	if (queryModel!=null) {
	    //用户已存在
	    responeModel.code="1002";
	    responeModel.message="用户已存在";

	    resultString = JSONSerializer.toJSON(responeModel).toString();
	} else {
	    //用户不存在 保存用户
	    try {
		//构建用户
		UserModel userModel = new UserModel();
		//标识 
		userModel.setUserUid(UUID.randomUUID().toString());
		//姓名 
		userModel.setUserName(userName);
		//昵称
		userModel.setNixe(nike);
		//性别
		userModel.setSex(sex);
		//密码
		userModel.setPassword(password);
		//注册类型
		userModel.setRegisterType(registerType);
		//头像
		userModel.setUserTitleImage(userTitleImage);
		//保存用户
		userDao.addUser(userModel);

		UserModel resultModel = userDao.queryModelFormName(userName);
		responeModel.content = resultModel;
		responeModel.code="1000";
		responeModel.message="注册成功";

		resultString = JSONSerializer.toJSON(responeModel).toString();

	    } catch (Exception e) {
		System.out.println("注册异常 "+e.getMessage());
	    }

	}
	return resultString;
    }

    /**
     * 登录
     */
    @Override
    public String loginFunction(HttpServletRequest request) {

	String resultString = "";
	ResponeModel<UserModel> responeModel = new ResponeModel<UserModel>();

	String userName = request.getParameter("userName");
	String password = request.getParameter("password");
	String loginTypeString = request.getParameter("loginType");


	if (userName==null||userName.equals("")) {
	    responeModel.code="1002";
	    responeModel.message="用户名为空 ... ";
	}else if (password==null||password.equals("")) {
	    responeModel.code="1002";
	    responeModel.message="密码为空 ... ";
	}


	if (userName==null||userName.equals("")) {

	    resultString = commonUserLoginFunction(resultString, responeModel,
		    userName, password);
	}else {
	    resultString = qqloginFunction(resultString, responeModel,
		    userName, password);
	}



	return resultString;
    }

    private String qqloginFunction(String resultString,
	    ResponeModel<UserModel> responeModel, String userName,
	    String password) {
	//查询用户 
	//查询用户是否存在 
	UserModel queryModel = userDao.queryModelFormNike(userName);
	if (queryModel==null) {
	    responeModel.code="1003";
	    responeModel.message="用户不存在，请注册";
	} else {
	    //核对密码
	    String password2 = queryModel.getPassword();
	    if (password2!=null&&!password2.equals("")) {

		if (password.equals(password2)) {
		    responeModel.code="1000";
		    responeModel.content=queryModel;
		    responeModel.message="登录成功";
		} else {
		    responeModel.code="1004";
		    responeModel.message="用户密码不正确";
		}

	    } else {
		responeModel.code="1002";
		responeModel.message="密码为空 ... ";
	    }

	}
	try {
	    resultString = JSONSerializer.toJSON(responeModel).toString();
	} catch (Exception e) {
	    System.out.println("登录异常 "+e.getMessage());
	}
	return resultString;
    }

    private String commonUserLoginFunction(String resultString,
	    ResponeModel<UserModel> responeModel, String userName,
	    String password) {
	//查询用户 
	//查询用户是否存在 
	UserModel queryModel = userDao.queryModelFormName(userName);
	if (queryModel==null) {
	    responeModel.code="1003";
	    responeModel.message="用户不存在，请注册";
	} else {
	    //核对密码
	    String password2 = queryModel.getPassword();
	    if (password2!=null&&!password2.equals("")) {

		if (password.equals(password2)) {
		    responeModel.code="1000";
		    responeModel.content=queryModel;
		    responeModel.message="登录成功";
		} else {
		    responeModel.code="1004";
		    responeModel.message="用户密码不正确";
		}

	    } else {
		responeModel.code="1002";
		responeModel.message="密码为空 ... ";
	    }

	}
	try {
	    resultString = JSONSerializer.toJSON(responeModel).toString();
	} catch (Exception e) {
	    System.out.println("登录异常 "+e.getMessage());
	}
	return resultString;
    }

    /*
     * 修改用户信息
     * (non-Javadoc)
     * @see android.longs.study.user.service.in.UserService#alertUserFunction(javax.servlet.http.HttpServletRequest)
     */
    @Override
    public String alertUserFunction(HttpServletRequest request) {

	//获取用户 uid 
	String userUid = request.getParameter("userUid");

	ResponeModel<UserModel> responeModel = new ResponeModel<>();
	UserModel userModel = userDao.queryModelFormUid(userUid);

	if (userUid==null||userUid.equals("")) {
	    responeModel.code="1002";
	    responeModel.message="用户UID为空";
	}else if(userModel==null){
	    responeModel.code="1003";
	    responeModel.message="查询用户异常";
	}else {

	    String userFlag = request.getParameter("userFlag");
	    String nixe = request.getParameter("nixe");

	    String identitflg = request.getParameter("identitflg");

	    //签名
	    if (userFlag!=null) {
		userModel.setUserFlag(userFlag);
	    }
	    if (nixe!=null) {
		userModel.setNixe(nixe);
	    }

	    if (identitflg!=null) {
		userModel.setIdentitflg(Integer.valueOf(identitflg));
	    }



	    responeModel.code="1000";
	    responeModel.content = userModel;
	    responeModel.message="修改成功";
	}




	return GsonUtils.getintance().toJson(responeModel, null);
    }

    @Override
    public String getSubject(HttpServletRequest request) {

	String subUid = request.getParameter("subUid");
	String userUid = request.getParameter("userUid");
	String resultString = "";
	System.out.println("查询试题 ... ... "+subUid+"  "+userUid);

	ResponeModel<UserSubjectModel> responeModel = new ResponeModel<>();

	if (subUid==null||subUid.equals("")||userUid==null||userUid.equals("")) {
	    responeModel.code="1002";
	    responeModel.message="试题id 或者用户的ID  为空";
	} else {

	    responeModel.code="1000";
	    responeModel.message="成功";
	    UserSubjectModel querySubModelFormUid = userDao.querySubModelFormUid(subUid,userUid);

	    responeModel.content = querySubModelFormUid;
	}

	try {
	    JsonConfig jsonConfig = new JsonConfig();
	    jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);

	    resultString = JSONSerializer.toJSON(responeModel,jsonConfig).toString();
	} catch (Exception e) {
	    System.out.println("获取试题 异常 "+e.getMessage());
	}

	return resultString;
    }

    @Override
    public String alertUserClassFunction(HttpServletRequest request) {

	String userId = request.getParameter("userId");
	String classId = request.getParameter("classId");
	String resultString = "";


	ResponeModel<String> responeModel = new ResponeModel<>();

	if (classId==null||classId.equals("")||userId==null||userId.equals("")) {
	    responeModel.code="1002";
	    responeModel.message="班级id 或者用户的ID  为空";
	} else {


	    UserModel userModel = userDao.queryModelFormUid(userId);
	    if (userModel==null) {
		responeModel.code="1003";
		responeModel.message="查询用户信息 异常 ";
		responeModel.content = "";
	    } else {
		responeModel.code="1000";
		userModel.setClassId(classId);
		userDao.updateModel(userModel);
		responeModel.message="成功";
		responeModel.content = "";
	    }

	}

	try {
	    JsonConfig jsonConfig = new JsonConfig();
	    jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);

	    resultString = JSONSerializer.toJSON(responeModel,jsonConfig).toString();
	} catch (Exception e) {
	    System.out.println("获取异常 "+e.getMessage());
	}

	return resultString;
    }
    // TODO  修改昵称
    @Override
    public String alertUserNikeFunction(HttpServletRequest request) {

	String userId = request.getParameter("userId");
	String nixe = request.getParameter("nixe");
	String resultString = "";


	ResponeModel<String> responeModel = new ResponeModel<>();

	if (nixe==null||nixe.equals("")||userId==null||userId.equals("")) {
	    responeModel.code="1002";
	    responeModel.message="班级id 或者用户的ID  为空";
	} else {


	    UserModel userModel = userDao.queryModelFormUid(userId);
	    if (userModel==null) {
		responeModel.code="1003";
		responeModel.message="查询用户信息 异常 ";
		responeModel.content = "";
	    } else {
		responeModel.code="1000";
		userModel.setNixe(nixe);
		userDao.updateModel(userModel);
		responeModel.message="成功";
		responeModel.content = "";
	    }

	}

	try {
	    JsonConfig jsonConfig = new JsonConfig();
	    jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);

	    resultString = JSONSerializer.toJSON(responeModel,jsonConfig).toString();
	} catch (Exception e) {
	    System.out.println("获取异常 "+e.getMessage());
	}

	return resultString;
    }
    // TODO 修改签名
    @Override
    public String alertUserFlagFunction(HttpServletRequest request) {

	String userId = request.getParameter("userId");
	String flag = request.getParameter("flag");
	String resultString = "";


	ResponeModel<String> responeModel = new ResponeModel<>();

	if (flag==null||flag.equals("")||userId==null||userId.equals("")) {
	    responeModel.code="1002";
	    responeModel.message="班级id 或者用户的ID  为空";
	} else {


	    UserModel userModel = userDao.queryModelFormUid(userId);
	    if (userModel==null) {
		responeModel.code="1003";
		responeModel.message="查询用户信息 异常 ";
		responeModel.content = "";
	    } else {
		responeModel.code="1000";
		userModel.setUserFlag(flag);
		userDao.updateModel(userModel);
		responeModel.message="成功";
		responeModel.content = "";
	    }

	}

	try {
	    JsonConfig jsonConfig = new JsonConfig();
	    jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);

	    resultString = JSONSerializer.toJSON(responeModel,jsonConfig).toString();
	} catch (Exception e) {
	    System.out.println("获取异常 "+e.getMessage());
	}

	return resultString;
    }

    //TODO 修改用户的密码 
    @Override
    public String alertUserPasswordFunction(HttpServletRequest request) {

	String userId = request.getParameter("userId");
	String flag = request.getParameter("password");
	String resultString = "";


	ResponeModel<String> responeModel = new ResponeModel<>();

	if (flag==null||flag.equals("")||userId==null||userId.equals("")) {
	    responeModel.code="1002";
	    responeModel.message="密码 或者用户的ID  为空";
	} else {


	    UserModel userModel = userDao.queryModelFormUid(userId);
	    if (userModel==null) {
		responeModel.code="1003";
		responeModel.message="查询用户信息 异常 ";
		responeModel.content = "";
	    } else {
		responeModel.code="1000";
		userModel.setPassword(flag);
		userDao.updateModel(userModel);
		responeModel.message="成功";
		responeModel.content = "";
	    }

	}

	try {
	    JsonConfig jsonConfig = new JsonConfig();
	    jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);

	    resultString = JSONSerializer.toJSON(responeModel,jsonConfig).toString();
	} catch (Exception e) {
	    System.out.println("获取异常 "+e.getMessage());
	}

	return resultString;
    }

    /**
     * QQ登录 
     * @param request
     * @return
     */
    public String qqloginFunction(HttpServletRequest request) {
	String userName = request.getParameter("name");
	String nike = request.getParameter("nike");
	String sex = request.getParameter("sex");
	String password = request.getParameter("password");
	String registerType = request.getParameter("registerType");
	String userTitleImage = request.getParameter("userTitleImage");

	System.out.println("name "+userName+"  nike  "+nike+"  sex "+sex+"  password "+password);

	String result = "";
	//查询用户是否存在 
	UserModel queryModel = userDao.queryModelFormNike(nike);
	if (queryModel==null) {
	    //注册
	    result = registerFunction(request);

	} else {
	    //登录
	    ResponeModel<UserModel> responeModel = new ResponeModel<UserModel>();

	    responeModel.code="1000";
	    responeModel.content=queryModel;
	    responeModel.message="登录成功";
	    try {
		result = JSONSerializer.toJSON(responeModel).toString();
	    } catch (Exception e) {
		System.out.println("登录异常 "+e.getMessage());
	    }

	}
	return result;
    }
}
